//
//  ZSBContactAreaMenu.m
//  HCNewGoldFinger
//
//  Created by zjhcsoft on 14-10-13.
//  Copyright (c) 2014年 zjhcsoftios. All rights reserved.
//

#import "ZSBContactAreaMenu.h"
#import "UIColor+EX.h"
#import "NetworkAddress.h"

@interface ZSBContactAreaMenu()

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UILabel *contentLab;

@end

@implementation ZSBContactAreaMenu

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpView];
    }
    return self;
}

- (void)setUpView
{
    _contentView = [[UIView alloc] initWithFrame:CGRectZero];
    _contentView.backgroundColor = [UIColor colorWithHexString:@"EFEFF4"];
    _contentView.clipsToBounds = YES;
    [self addSubview:_contentView];
    
    _contentLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth-20, 32)];
    _contentLab.font = [UIFont systemFontOfSize:13];
    _contentLab.textColor = [UIColor colorWithHexString:@"999999"];
    _contentLab.backgroundColor = [UIColor clearColor];
    [_contentView addSubview:_contentLab];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
    line.backgroundColor = [UIColor colorWithHexString:@"cccccc"];
    [_contentView addSubview:line];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 32-0.5, kScreenWidth, 0.5)];
    line1.backgroundColor = [UIColor colorWithHexString:@"cccccc"];
    [_contentView addSubview:line1];
}

- (void)setDateList:(NSArray *)dateList
{
    _dateList = dateList;
    for (UIButton *subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]){
            [subView removeFromSuperview];
        }
    }
    if (_dateList == nil) {
        return;
    }
    for (int i = 0; i < _dateList.count; i++) {
        int row = i/4;
        int x = i-row*4;
        
        ZSBContactInfo *contactInfo = [_dateList objectAtIndex:i];
        
        NSString *buttonName = contactInfo.companyName;
        if (buttonName.length>2 && i>0) {
            buttonName = [buttonName substringWithRange:NSMakeRange(0, 2)];
        }
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        button.frame = CGRectMake(((kScreenWidth - 280) /5 + 70) * x+(kScreenWidth - 280) / 5, 40 * row + 10, 70, 30);
        [button setBackgroundImage:[self buttonImageFromColor:[UIColor whiteColor] andSize:CGSizeMake(70, 30)] forState:UIControlStateNormal];
        [button setBackgroundImage:[self buttonImageFromColor:[UIColor colorWithHexString:@"db5a47"] andSize:CGSizeMake(70, 30)] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setTitle:buttonName forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        if (i == self.myIndex) {
            [self buttonClick:button];
        }
    }
    int rows = _dateList.count/4;
    if (_dateList.count%4==0 && _dateList.count!=0) {
        rows--;
    }
    _contentView.frame = CGRectMake(0, rows*40+50, kScreenWidth, 32);
    self.frame = CGRectMake(0, 50, kScreenWidth, rows*40+50+32);
}

- (void)buttonClick:(UIButton *)button
{
    for (UIButton *subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            if (subView.tag == button.tag) {
                subView.selected = YES;
                
                ZSBContactInfo *contactInfo = [_dateList objectAtIndex:button.tag];
                _contentLab.text = contactInfo.companyName;
                
                if ([_delegate respondsToSelector:@selector(menuDidSelectWithInfo:andIndex:)]) {
                    [_delegate menuDidSelectWithInfo:contactInfo andIndex:button.tag];
                }
                
            }else{
                subView.selected = NO;
            }
        }
    }
}

- (UIImage *)buttonImageFromColor:(UIColor *)color andSize:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

@end
